package com.drama.api.company.company.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.drama.api.base.Constants;
import com.drama.api.base.Type;
import com.drama.api.company.company.dao.CompanyDao;
import com.drama.api.company.company.domain.po.CompanyPO;
import com.drama.api.company.company.domain.vo.CompanyLoginReqVO;
import com.drama.api.company.company.domain.vo.CompanyLoginResVO;
import com.drama.api.company.company.domain.vo.CompanyPasswordUpdateVO;
import com.drama.api.company.company.domain.vo.CompanyVO;
import com.drama.api.company.company.service.ICompanyService;
import com.drama.exception.BusinessException;
import com.drama.util.ToolUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;


@Service
@Transactional
@Slf4j
public class CompanyServiceImpl implements ICompanyService {

    @Autowired
    private CompanyDao companyDao;

    @Override
    public CompanyLoginResVO login(CompanyLoginReqVO companyLogin) {
        Example example = new Example(CompanyPO.class);
        example.and().andEqualTo("companyAccount",companyLogin.getLoginName())
                .andEqualTo("delFlag", false);
        CompanyPO loginUser=companyDao.selectOneByExample(example);
        if(loginUser==null){
            throw new BusinessException(Type.NOT_FOUND_ERROR.getCode(),"商户信息不存在！");
        }
        if(!loginUser.getCompanyPassword().equals(companyLogin.getPassword())){
            throw new BusinessException(Type.FAIL.getCode(),"账号密码错误！");
        }
        if(loginUser.getIsLogin()!=1){
            throw new BusinessException(Type.FAIL.getCode(),"账号已被禁用！");
        }
        StpUtil.login(loginUser.getId());
        CompanyLoginResVO userLoginResVO =new CompanyLoginResVO();
        userLoginResVO.setId(loginUser.getId());
        userLoginResVO.setToken(StpUtil.getTokenValue());
        userLoginResVO.setCompanyName(loginUser.getCompanyName());
        return userLoginResVO;
    }

    @Override
    public CompanyVO getOne() {
        CompanyPO companyPO=companyDao.selectByPrimaryKey(StpUtil.getLoginId());
        if(companyPO==null){
            throw new BusinessException(Type.NOT_FOUND_ERROR.getCode(),"商户信息不存在！");
        }
        CompanyVO companyVO=new CompanyVO();
        BeanUtils.copyProperties(companyPO, companyVO);
        String s="";
        if(new BigDecimal(companyPO.getFlowNum()).compareTo(BigDecimal.ZERO)==-1){
            s="-";
        }
        companyVO.setResidueFlowNum(s+ToolUtil.formatFileSize(new BigDecimal(companyPO.getFlowNum()).abs().toString()));
        companyVO.setResidueStorageNum(ToolUtil.formatFileSize(new BigDecimal(companyPO.getMaxStorage()).subtract(new BigDecimal(companyPO.getStorageNum())).longValue()));
        companyVO.setStorageNum(ToolUtil.formatFileSize(new BigDecimal(companyPO.getStorageNum()).longValue()));
        return companyVO;
    }

    @Override
    public void updatePassword(CompanyPasswordUpdateVO passwordUpdateVO) {
        CompanyPO companyPO = companyDao.selectByPrimaryKey(StpUtil.getLoginIdAsString());
        if (companyPO == null) {
            throw new BusinessException(Type.NOT_FOUND_ERROR.getCode(),  "用户信息不存在");
        }
        if(!companyPO.getCompanyPassword().equals(passwordUpdateVO.getOlePassword())){
            throw new BusinessException(Type.NOT_FOUND_ERROR.getCode(),  "密码修改失败，旧密码不正确");
        }
        companyPO.setCompanyPassword(passwordUpdateVO.getNewPassword());
        companyPO.preUpdate();
        int i = companyDao.updateByPrimaryKeySelective(companyPO);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }


}